package client

import (
	"encoding/json"
	"errors"
	"gitee.com/byx_darwin/ncloud-go/core"
	"gitee.com/byx_darwin/ncloud-go/core/net/consts"
	"gitee.com/byx_darwin/ncloud-go/services/cdn/apis"
	"github.com/bytedance/sonic"
)

type CdnClient struct {
	core.NCloudClient
}

func NewCdnClient(credential *core.Credential) *CdnClient {
	if credential == nil {
		return nil
	}
	config := core.NewConfig()
	config.SetScheme(consts.SchemeHttps)
	config.SetEndpoint("xycdn.api.xyuncloud.com")
	//config.SetScheme(consts.SchemeHttp)
	//config.SetEndpoint("111.230.246.19:30012")
	return &CdnClient{
		core.NCloudClient{
			Credential:  *credential,
			Config:      *config,
			ServiceName: "cdn",
			Revision:    core.Version,
			Logger:      core.NewDefaultLogger(core.LogInfo),
		}}
}

func (c *CdnClient) DisableLogger() {
	c.Logger = core.NewDummyLogger()
}

// QueryDomain /* 查询域名 */
func (c *CdnClient) QueryDomain(request *apis.QueryDomainRequest) (*apis.QueryDomainResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	c.Logger.Log(core.LogInfo, "Request: %s", request.GetPath())
	resp, err := c.Send(request.Method, request.Path, "")
	if err != nil {
		return nil, err
	}
	jsonResp := &apis.QueryDomainResponse{}
	err = json.Unmarshal(resp, jsonResp)
	if err != nil {
		c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
		return nil, err
	}
	return jsonResp, err
}

// QueryIsp /* 查询运营商 */
func (c *CdnClient) QueryIsp(request *apis.QueryIspRequest) (*apis.QueryIspResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	resp, err := c.Send(request.Method, request.Path, "")
	if err != nil {
		return nil, err
	}
	jsonResp := &apis.QueryIspResponse{}
	err = json.Unmarshal(resp, jsonResp)
	if err != nil {
		c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
		return nil, err
	}
	return jsonResp, err
}

// QueryRegion /* 查询地区*/
func (c *CdnClient) QueryRegion(request *apis.QueryRegionRequest) (*apis.QueryRegionResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	resp, err := c.Send(request.Method, request.Path, "")
	if err != nil {
		return nil, err
	}
	jsonResp := &apis.QueryRegionResponse{}
	err = json.Unmarshal(resp, jsonResp)
	if err != nil {
		c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
		return nil, err
	}
	return jsonResp, err
}

// QueryContent /* 查询内容 */
func (c *CdnClient) QueryContent(request *apis.QueryContentRequest) (*apis.QueryContentTaskResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	resp, err := c.Send(request.Method, request.GetPath(), "")
	if err != nil {
		return nil, err
	}
	jsonResp := &apis.QueryContentTaskResponse{}
	err = json.Unmarshal(resp, jsonResp)
	if err != nil {
		c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
		return nil, err
	}
	return jsonResp, err
}

// QueryMetric /* 查询指标 */
func (c *CdnClient) QueryMetric(request *apis.QueryMetricRequest) (*apis.QueryMetricResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	jsonDataByte, err := sonic.Marshal(request.QueryMetricConditions)
	if err != nil {
		return nil, err
	}
	resp, err := c.Send(request.Method, request.GetPath(), string(jsonDataByte))
	if err != nil {
		return nil, err
	}

	jsonResp := &apis.QueryMetricResponse{}
	err = sonic.Unmarshal(resp, jsonResp)
	if err != nil {
		c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
		return nil, err
	}
	return jsonResp, err
}

// QueryFlow /* 查询域名流量 */
func (c *CdnClient) QueryFlow(request *apis.QueryFlowRequest) (*apis.QueryFlowResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	jsonDataByte, err := sonic.Marshal(request.QueryFlowConditions)
	if err != nil {
		return nil, err
	}
	resp, err := c.Send(request.Method, request.GetPath(), string(jsonDataByte))
	if err != nil {
		c.Logger.Log(core.LogError, "resp: %s", string(resp))
		return nil, err
	}

	jsonResp := &apis.QueryFlowResponse{}
	err = sonic.Unmarshal(resp, jsonResp)
	if err != nil {
		c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
		return nil, err
	}
	return jsonResp, err
}

// QuerySrc /* 查询回源 */
func (c *CdnClient) QuerySrc(request *apis.QuerySrcRequest) (*apis.QuerySrcResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	jsonDataByte, err := sonic.Marshal(request.QuerySrcConditions)
	if err != nil {
		return nil, err
	}
	resp, err := c.Send(request.Method, request.GetPath(), string(jsonDataByte))
	if err != nil {
		return nil, err
	}

	jsonResp := &apis.QuerySrcResponse{}
	err = sonic.Unmarshal(resp, jsonResp)
	if err != nil {
		c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
		return nil, err
	}
	return jsonResp, err
}

// CreatePreloadContent /* 创建预热内容 */
func (c *CdnClient) CreatePreloadContent(request *apis.CreatePreloadRequest) (*apis.CreatePreloadContentResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	jsonDataByte, err := sonic.Marshal(request.CreatePreloadConditions)
	if err != nil {
		return nil, err
	}
	resp, err := c.Send(request.Method, request.GetPath(), string(jsonDataByte))
	if err != nil {
		return nil, err
	}

	jsonResp := &apis.CreatePreloadContentResponse{}
	err = sonic.Unmarshal(resp, jsonResp)
	if err != nil {
		c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
		return nil, err
	}
	return jsonResp, err
}

// CreateRefreshContent /* 创建刷新内容 */
func (c *CdnClient) CreateRefreshContent(request *apis.CreateRefreshRequest) (*apis.CreateRefreshContentResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	jsonDataByte, err := sonic.Marshal(request.CreateRefreshConditions)
	if err != nil {
		return nil, err
	}
	resp, err := c.Send(request.Method, request.GetPath(), string(jsonDataByte))
	if err != nil {
		return nil, err
	}

	jsonResp := &apis.CreateRefreshContentResponse{}
	err = sonic.Unmarshal(resp, jsonResp)
	if err != nil {
		c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
		return nil, err
	}
	return jsonResp, err
}
